let hotSpot = {
    init() {
        let that = this;
        this.ctx = 'http://192.168.25.55:9999';
        this.hotspotListPage = 0;//热点列表、详情的翻译按钮
        this.discoveryPage = 0;//热点列表、详情的翻译按钮
        this.filterParam = {};//热点列表、详情的翻译按钮
        this.switch();//热点列表、详情的翻译按钮
        this.filterClick();//热点列表、详情的翻译按钮
        this.hotspotList();//构建热点列表
        this.discoveryList();//构建热点列表
        this.loadMore();//下拉加载
    },
    switch() {
        $(".switch-btn-item").click(function () {
            $(this).siblings().removeClass('active');
            $(this).addClass('active');
        })
    },
    filterClick() {
        let that = this;
        $(".filter-item-list .item").click(function () {
            $(this).siblings().removeClass('active');
            $(this).addClass('active');
            let oKey = $(this).parent().attr('id');
            let oValue = $(this).text();
            that.filterParam[oKey] =oValue;
            let country=that.filterParam.country;
            let domain=that.filterParam.domain;
            let language=that.filterParam.language;
            let emotional=that.filterParam.emotional;
            $('#discovery .news-list').html('');
            that.discoveryList(country,domain,language,emotional);
        })
    },
    hotspotList() {
        this.hotspotListPage++;
        $.ajax({
            headers: {
                "token": Public.token
            },
            url: Public.ctx + '/api/news/list',
            type: 'get',
            dataType: 'json',
            data: {
                //country: country,
                //domain: domain,
                //startDate: startDate,
                //endDate: endDate,
                //sort: sort,
                pageNum: this.hotspotListPage,
                pageSize: 10
            },
            success: (data) => {
                if(data.success === true){
                    var newsArr = data.data.maps;
                    //console.log(data);
                    var str = '';
                    $.each(newsArr, function (i, item) {
                        title = item.translatitle;
                        content = item.translate;
                        str += `<a href="/work_xiiwaiWeixin/pages/details.html?_ijt=1fsbvj0qpc5s0jems6bm51ogqg&id=${item.esId}&type=${item.type}&index=${item.index}" target="_blank" class="weui-media-box weui-media-box_appmsg">
                                <div class="weui-media-box_text">
                                <h4 class="weui-media-box__title">${title ? title : '---'}<i class="icon_new"></i></h4>
                                <p class="weui-media-box__desc">${content.length > 120 ? content.substring(0, 120) + '...' : content}</p>
                                <ul class="weui-media-box__info">
                                    <li class="weui-media-box__info__meta">${item.areacity ? item.areacity : '--'}</li>
                                    <li class="weui-media-box__info__meta">${item.release ? item.release : '--'}</li>
                                    <li class="weui-media-box__info__meta weui-media-box__info__meta_time">${item.time ? item.time.substr(0, 10) : '--'}</li>
                                </ul>
                                </div>
                            </a>`
                    });
                    $('#hotSpot .news-list').append(str);
                }else {
                    Public.message();
                }
            }
        })
    },
    discoveryList(country,domain,language,emotional) {
        this.discoveryPage++;
        $.ajax({
            headers: {
                "token": Public.token
            },
            url: Public.ctx + '/api/aggregate/discovery/list',
            type: 'get',
            dataType: 'json',
            data: {
                country: country?country:'',
                domain: domain?domain:'',
                language:language?language:'',
                emotional:emotional?emotional:'',
                pageNum: this.discoveryPage,
                pageSize: 10
            },
            success: (data) => {
                if(data.success === true){
                    var newsArr = data.data.maps;
                    var total = data.data.total;
                    let str = '';
                    if(total){
                        $('.weui-loadmore').remove();
                        $.each(newsArr, function (i, item) {
                            str += `<a href="/api/v1/view?to=modules/hotspot/hotspotNewsDetail&id=${item.esId}&type=${item.type}&index=${item.index}" target="_blank" class="weui-media-box weui-media-box_appmsg">
                                    <div class="weui-media-box_text">
                                        <h4 class="weui-media-box__title">${item.translatitle ? item.translatitle : '---'}<i class="icon_new"></i></h4>
                                        <p class="weui-media-box__desc">${item.preface}</p>
                                        <ul class="weui-media-box__info">
                                            <li class="weui-media-box__info__meta">${item.areacity ? item.areacity : '--'}</li>
                                            <li class="weui-media-box__info__meta">${item.release ? item.release : '--'}</li>
                                            <li class="weui-media-box__info__meta weui-media-box__info__meta_time">${item.time ? item.time.substr(0, 10) : '--'}</li>
                                        </ul>
                                    </div>
                                </a>`
                        });
                        str +=`<div class="weui-loadmore">
                                    <i class="weui-loading"></i>
                                    <span class="weui-loadmore__tips">正在加载</span>
                                </div>`
                    }else {
                        str +=`<div class="weui-loadmore weui-loadmore_line">
                                  <span class="weui-loadmore__tips">暂无数据</span>
                                </div>`
                    }

                    $('#discovery .news-list').append(str);
                }else {
                    Public.message();
                }
            }
        })
    },
    hotspotDetail(){
        $.ajax({
            headers: {
                "token": Public.token
            },
            url: Public.ctx + '/api/content/detail/${newsId}',
            type: 'get',
            dataType: 'json',
            data: {

            },
            success: (data) => {
                if(data.success === true){

                }else {
                    Public.message();
                }
            }
        })
    },
    loadMore(){
        let that=this;
        $(".infinite").infinite().on("infinite", function() {
            var self = this;
            if(self.loading) return;
            //console.log(self)
            self.loading = true;
            //console.log(self);
            setTimeout(function() {
                if($(self).attr('id') == 'hotSpot'){
                    that.hotspotList();
                }
                if($(self).attr('id') == 'discovery'){
                    that.discoveryList();
                }
                self.loading = false;
            }, 1500);   //模拟延迟
        });
    }
};
hotSpot.init();
